package c03.thread.api;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

/*
1. 调用 yield 会让当前线程从 Running 进入 Runnable 就绪状态,然后调度执行其它线程
2. 具体的实现依赖于操作系统的任务调度器
 */
@Slf4j(topic = "c.ThreadYieldTest")
public class ThreadYieldTest {
    public static void main(String[] args) {
        Runnable task1 = () -> {
            for (int i = 0; i < 1000; i++) {
                if (i % 10 == 0) {
                    System.out.println("task1: " + i);
                }
            }
        };

        Runnable task2 = () -> {
            for (int i = 0; i < 1000; i++) {
                if (i % 10 == 0) {
                    System.out.println("            task2: " + i);
                    if (i % 100 == 0) {
                        Thread.yield();
                        System.out.println("task2 yield");
                    }
                }
            }
        };

        Thread t1 = new Thread(task1, "t1");
        Thread t2 = new Thread(task2, "t2");
        t1.start();
        t2.start();
    }

    @Test
    public void test() {
        System.out.println(110 % 100);
    }
}
